/* **************************************************************************/
/*COPYRIGHT CCWIN-IN ALL RIGHTS RESERVED. THIS IS AN UNPUBLISHED WORK.      */
/*V8:ConvertMode=Report                                                     */
/*V8:WebEnabled=Yes                                                         */
/*V8:RunMode=Character,Windows                                              */
/****************************************************************************/
/* PROCEDURE NAME        : xxsoinvapi.p                                     */
/* PROCEDURE TYPE        : API CIMLOAD PROGRAM                              */
/* DESCRIPTION           : Salse Invoice API                                */
/*                                                                          */     
/* INCLUDE FILES         :                                                  */
/* CALLED BY             :                                                  */
/* CALLED PROCEDURES     :                                                  */
/****************************************************************************/
/* CREATED BY            : Gaoyan                               22-OCT-24   */
/****************************************************************************/ 
/****************************************************************************/
/*                                              */
/****************************************************************************/


{us/mf/mfdtitle.i}
{us/bbi/gplabel.i}
{us/wb/wbrp01.i}

{us/xx/xxgetframevalue.i}
{us/xx/xxcrtcip.i}

{xxjson.i}
{xxcimload.i}

{xxapilog.i}




&SCOPED-DEFINE TimeStamp1 substring(string(year(today), "9999"),3) + string(month(today), "99") + string(day(today), "99")
&SCOPED-DEFINE TimeStamp2 substring(string(time, "HH:MM:SS"), 1, 2) + substring(string(time, "HH:MM:SS"), 4, 2) + substring(string(time, "HH:MM:SS"), 7, 2)


define input parameter vc_inputjson             as character format "x(60)" no-undo.
define input parameter vc_outputjson            as character format "x(60)" no-undo.
define input parameter vc_import                as logical  no-undo.


define variable viReturn                     as integer     no-undo.
define variable ilReturnDataset              as logical     no-undo.
define variable ilPartialUpdate              as logical     no-undo.
define variable icPartialUpdateExceptionList as character   no-undo.
define variable ocPrimaryKey                 as character   no-undo.
define variable ocRowid                      as character   no-undo.
define variable oiDraftInstance              as integer     no-undo.
define variable ocPrimaryKeyName             as character   no-undo.
define variable vho                          as handle.
define variable oiReturnStatus               as integer     no-undo.

DEFINE VARIABLE li_access           AS    INTEGER                       NO-UNDO.
define variable l_file                       as character format "x(40)" no-undo.
define variable l_line                       as character   no-undo.
define variable lvi_row_id                   as decimal     no-undo.
define variable v_import                     as logical     init   "no"      no-undo.
define variable v_site                       like si_site   no-undo.
DEFINE VARIABLE li_year                      LIKE  posting.postingyear                NO-UNDO.
DEFINE VARIABLE li_period                    LIKE  posting.postingperiod                NO-UNDO.
DEFINE VARIABLE li_cur_yr                    LIKE  glc_year                      NO-UNDO.
DEFINE VARIABLE li_cur_per                   LIKE  glc_per                       NO-UNDO.
DEFINE VARIABLE v_fromloc                    LIKE  loc_loc                       NO-UNDO.
DEFINE VARIABLE v_toloc                      LIKE  loc_loc                       NO-UNDO.
DEFINE VARIABLE ld_start_dt                  LIKE  glc_start                     NO-UNDO.
DEFINE VARIABLE ld_end_dt                    LIKE  glc_end                       NO-UNDO.
define variable v_cust                       like cm_addr   no-undo.
define variable R_import                     as logical     init   "No"         no-undo.

define variable l_error                      as character                      no-undo.
define stream f_log.
define stream f_salecim.
define stream f_pscim.
define variable v_quote                   as character initial """"              no-undo.
define variable v_default                 as character initial "-"               no-undo.
define variable v_space                   as character initial " "               no-undo.
define variable v_salelog                 as character                           no-undo.
define variable v_salecim                 as character                           no-undo.
define variable v_pslog                   as character                           no-undo.
define variable v_pscim                   as character                           no-undo.
define variable v_fline                   as character                           no-undo.
define variable v_saleerr                 as logical                             no-undo.
define variable v_errstring               as character                           no-undo.
define variable v_status                  as character                           no-undo.
define variable v_existpod                as logical          initial no         no-undo.
define variable v_existpo                 as logical          initial no         no-undo.
define variable v_poopen                  as logical          initial no         no-undo.
define variable v_podopen                 as logical          initial no         no-undo.
define variable v_sched                   as logical          initial no         no-undo.
define variable v_defaultloc              as logical          initial no         no-undo.
define variable v_entity                  like si_entity                         no-undo.
define variable v_filepath                as character        format "X(40)"     no-undo.
define variable v_filename                as character        format "X(40)"     no-undo.
define variable v_file                    as character        format "x(20)"     no-undo.
define variable v_file1                   as character        format "x(20)"     no-undo.
define variable v_prefix                  as character        format "x(20)"     no-undo.
define variable v_extn                    as character                           no-undo.
define variable v_count                   as integer                             no-undo.
define variable v_flcnt                   as integer                             no-undo.
define variable dir-file                  as character                           no-undo.
define variable l_fdata                   as character                           no-undo.
define variable m_tmpstr                  as character                           no-undo.
define variable v_taxrate                 as decimal                             no-undo.
define variable v_taxadj                  as decimal                             no-undo.
define variable v_taxc                    as character                           no-undo.
define variable v_totinv                  as decimal                             no-undo.
define variable v_linestart               as date      initial 01/01/19          no-undo.
define variable v_tottax                  as decimal                             no-undo.
define variable m_arg1                    as character                           no-undo.
define variable v_err                     as logical  extent 8                   no-undo.
define variable v_rctqty                  as decimal                             no-undo.
define variable v_invpath                 as character                           no-undo.
define variable v_invfile                 as character                           no-undo.
define stream   data-stream.
define variable v_name                    as character                           no-undo.
define variable cst_date                  like tr_effdate                        no-undo.     
define variable trrecno                   as   recid                             no-undo.
define variable i                         as  integer                            no-undo.
define variable v_count1                  as  integer                            no-undo.
define variable v_sonbr                   as character                           no-undo.
define variable v_errorstatus             as logical                             no-undo.
define variable v_errorstatus1            as character                             no-undo.
define variable v_errornum                as character                           no-undo.
define variable v_Incomeacc               as character                           no-undo.
define variable v_negative                as logical                             no-undo.
define variable v_toterrorstatus          as logical                             no-undo.
                                                                                 
define variable vi_filecount              as integer                             no-undo.
define variable vc_filename               as character                           no-undo.
define variable vc_topath                 as character                           no-undo.
define variable v_runproagram             as character                           no-undo.
define variable vc_traceid                as character                           no-undo.
define variable v_errorcnt                as integer                             no-undo.


define temp-table ttsoinvdet no-undo
      
	   field so_nbr            like  so_mstr.so_nbr
	   field so_cust           like  so_mstr.so_cust
      field so_bill           like  so_mstr.so_bill
      field so_ship           like  so_mstr.so_ship               
      field so_ord_date       like  so_mstr.so_ord_date
      field so_po             like  so_mstr.so_po
      field so_rmks           like  so_mstr.so_rmks
      field so_site           like  so_mstr.so_site                 
      field so_channel        like  so_mstr.so_channel
      field so_project        like  so_mstr.so_project
      field so_ship_date      like  so_mstr.so_ship_date
      field so_curr           like  so_mstr.so_curr                  
      field so_taxable        like  so_mstr.so_taxable
      field so_taxc           like  so_mstr.so_taxc                  
      field sod_line          like  sod_det.sod_line                 
      field sod_part          like  sod_det.sod_part                 
      field sod_qty_inv       like  sod_det.sod_qty_inv		
      field sod_list_pr       like  sod_det.sod_list_pr 
	   field sod_loc           like  sod_det.sod_loc
	   field so_totivnamt      as    decimal
	   field so_tottaxamt      as    decimal
	   field so_invvaramt      as    decimal
	   field so_taxvaramt      as    decimal
	   field so_std_mtl        as    decimal
	   field so_std_lbr        as    decimal
	   field so_std_bdn        as    decimal
	   field so_std_ovh        as    decimal
	   field so_std_sub        as    decima
	   field so_filename       as    character
	   field so_error          as    character
	   field so_traceid        as    character
	   field so_dataid         as    character
	   field so_cimfilename         as    character
	   field so_logfilename         as    character

	  
      index sonbr so_nbr
	   index sopart so_nbr sod_part.
	  
   define buffer b_ttsoinvdet for ttsoinvdet.
   
define temp-table ttsonbr no-undo
   field tt_sonbr as   character
   field tt_cust  like ad_addr.
   
   DEFINE DATASET dsSoInvDet FOR ttsoinvdet.

   hdataset = dataset dsSoInvDet:HANDLE.             
    
   /* Output and Batch Input */
   
   {us/xx/xxworkpath_e.i &outbound=YES &inbound=YES &archive=YES}
   m_workpath = m_workpath + "cinv/".
   m_archpath = m_archpath + "cinv/".
   m_outpath = m_outpath + "cinv/".
   m_inpath = m_inpath + "cinv/".  
    v_filepath = m_inpath.
    
    
mainloop:
repeat on error undo mainloop, retry mainloop:
  
   empty temp-table ttsoinvdet.
     v_runproagram = "".
     vc_traceid = "".
   /* Load item master data from JSON  */ 
   vc_outputjson = replace(vc_inputjson,".json",".run.out.json").   
   run loadjsonfile(input vc_inputjson,
                    input vc_outputjson,
                    output vl_RetOK).
   if vl_RetOK then
   do:
      find first ttsoinvdet no-error.
      assign vc_traceid = ttsoinvdet.so_traceid.
   end.
  

   
      /*VALIDATE FILE NAME*/
	  
   /* Output and Batch Input */
   
  
   find code_mstr where code_mstr.code_fldname = "xxunbillcinv"
                    and code_mstr.code_domain = global_domain
                    and code_mstr.code_value  = "script_path"
                    no-lock no-error.
   if available code_mstr then 

   assign v_invpath = code_mstr.code_cmmt.
  
    else do:
          /*GCM NOT DEFINED FOR  SERVER PATH */      
          
        v_errorcnt = v_errorcnt + 1.
                  create api_status.
                  assign api_status.data_seq     = v_errorcnt
                         api_status.return_status  = "FAILED"
                         api_status.message_type = "Pre-validation"
                         api_status.message_string  = "script_path " +
                                                      GetTermLabel("GENERALIZED_CODE",8) +
                                                      GetTermLabel("DOES_NOT_EXIST",8) 
                                                                    
                         api_status.dataid  = ttsoinvdet.so_dataid   
                         api_status.billnbr = ttsoinvdet.so_nbr
                         api_status.traceid = vc_traceid 
                         api_status.domain = global_domain.  
    end.

	/* Validation - 'IC' Inventory module is open for the effective date */
         
		 FOR FIRST company 
            WHERE company.companycode = v_entity,
         FIRST period
            WHERE period.company_id       = company.company_id
            AND   period.periodstartDate <= ld_end_dt
            AND   period.periodenddate   >= ld_end_dt
         NO-LOCK:
		
            IF period.periodclosingdate <> ? 
            THEN DO:
            
            v_errorcnt = v_errorcnt + 1.
                  create api_status.
                  assign api_status.data_seq     = v_errorcnt
                         api_status.return_status  = "FAILED"
                         api_status.message_type = "Pre-validation"
                         api_status.message_string  = "GL Period not open" 
                                                                    
                         api_status.dataid  = ttsoinvdet.so_dataid   
                         api_status.billnbr = ttsoinvdet.so_nbr
                         api_status.traceid = vc_traceid 
                         api_status.domain = global_domain.  
               /* GL Period not open for effective date 
               {us/bbi/pxmsg.i &msgnum=13751 &errorlevel=4}
               */
            END. /* IF periodclosingdate <> ? */
            IF NOT PeriodIsPostingInvAllowed  
            THEN DO:
            
              v_errorcnt = v_errorcnt + 1.
                  create api_status.
                  assign api_status.data_seq     = v_errorcnt
                         api_status.return_status  = "FAILED"
                         api_status.message_type = "Pre-validation"
                         api_status.message_string  =  "Period is locked for inventory" 
                                                                    
                         api_status.dataid  = ttsoinvdet.so_dataid   
                         api_status.billnbr = ttsoinvdet.so_nbr
                         api_status.traceid = vc_traceid 
                         api_status.domain = global_domain.  				

			   
			   
               /* Period is locked for inventory related transactions 
               {us/bbi/pxmsg.i &msgnum=13750 &errorlevel=4}
			   */
            END. /* IF NOT PeriodIsPostingInvAllowed */
         END.  /* FOR FIRST company */

   /* when import is yes */
  if vc_import then do:  
   /* create cim load format */
  
    for each ttsoinvdet where ttsoinvdet.so_error = "" break by ttsoinvdet.so_nbr.

  	 if first-of(ttsoinvdet.so_nbr) then do:
	 repeat i = 1 to 8.
	 v_err[1] = yes.
	 end.
    
     /* Get next SO number */
	  v_sonbr = "".
	  
    {us/bbi/gprun.i ""gpnrmgv.p""
                "(""SOINVNBR"",
                input-output v_sonbr,
                output v_errorstatus1,
                output v_errornum)"}
    
	 /* For 7.13.1 order */
    v_salecim = m_workpath + "INV" + "_" + ttsoinvdet.so_traceid + string(ttsoinvdet.so_nbr) + "_" + {&TimeStamp1} + "_" + {&TimeStamp2}  + ".cim".
	 v_salelog = m_workpath + "INV" + "_" + ttsoinvdet.so_traceid + string(ttsoinvdet.so_nbr) + "_" + {&TimeStamp1} + "_" + {&TimeStamp2}  + ".log".
    output stream f_salecim to value(v_salecim).
     
    /* for 7.13.4 */
	 v_pscim = m_workpath + "PS" + "_" + ttsoinvdet.so_traceid + string(ttsoinvdet.so_nbr) + "_" + {&TimeStamp1} + "_" + {&TimeStamp2}  + ".cim".
	 v_pslog = m_workpath + "PS" + "_" + ttsoinvdet.so_traceid + string(ttsoinvdet.so_nbr) + "_" + {&TimeStamp1} + "_" + {&TimeStamp2}  + ".log".
    output stream f_pscim to value(v_pscim).
	/* Invoice Post CIM LOAD */
	 put stream f_pscim    unformatted    v_quote v_sonbr      v_quote  v_space      /* Sales order */
	                                      v_quote v_sonbr      v_quote  v_space      /* Sales order */
										           v_default v_space                               
										           v_default v_space
										           v_default v_space
										           v_default v_space
										           v_default v_space
										           v_default v_space
										           v_default v_space
										           v_default v_space
										           v_default v_space
										           v_default v_space
										           v_quote ttsoinvdet.so_site v_quote v_space   /* Site  */
										           v_quote ttsoinvdet.so_site v_quote v_space   /* Site  */
										           v_default v_space
										           v_default v_space
										           v_quote ttsoinvdet.so_ord_date  v_quote  v_space /* Effective date */
                                         v_quote "Y" v_quote v_space
                                         v_quote "N" v_quote v_space                     /* GL detail */
										           v_default v_space		                          /* Error summary */
										           v_default v_space		                          /* Debit Invoice  */
										           v_default v_space		                          /* Credit Invoice */
										           v_default v_space		                          /* Combine Invoice */
										           v_default v_space		                          /* correction invoice */
										           v_default v_space		                          /* Print correction invoice */
										           v_default v_space		                          /* Print invoice */
										           v_default v_space		                          /* Print ISB detail */
										           skip
										           v_quote v_sonbr v_quote v_space	  /* Print information */
										           v_quote "page" v_quote  skip.             	  /* Print invoice*/
										  
										  
	/* Sales order Head */ 
	 put stream f_salecim  unformatted    v_quote v_sonbr      v_quote skip    /* Invoice number */
	                                      v_quote ttsoinvdet.so_cust     v_quote skip    /* Customer Code  */
	                                      v_quote ttsoinvdet.so_bill     v_quote skip    /* Bill  To       */
										           v_default skip                                   /* Ship  TO       */
										           v_default   v_space                              /* Order Date     */
										           v_default   v_space                              /* REQUEST date   */
										           v_default   v_space                              /* Due     date   */
										           v_default   v_space                              /* PERFOR  date   */
										           v_default   v_space                              /* Price   date   */
										           v_quote ttsoinvdet.so_po v_quote v_space       /* Purchase order */
										           v_default   v_space                              /* remark         */
										           "NO"   v_space                              /* Project  price */
										           v_quote v_quote   v_space                        /* Price List     */
										           v_quote ttsoinvdet.so_site v_quote v_space     /* Site           */
										           v_default   v_space                              /* Channel        */
										           v_default   v_space                              /* Project        */
										           ttsoinvdet.so_ship_date   v_space              /* last ship      */
										           v_default   v_space                              /* Currency       */
										           v_default   v_space                              /* LANGUAGE       */
										           "Y"   v_space                              /* Taxable        */
										           ttsoinvdet.so_taxc   v_space                              /* Tax Rate       */
										           v_default   v_space                   /* Tax date       */
										           v_default   v_space                              /* Fixed Price    */
										           v_default   v_space                              /* Credit Term    */
										           v_default   v_space                              /* Pay interest   */
										           v_default   v_space                              /* Journal set    */
										           skip.
	  put stream f_salecim unformatted   v_default skip.                                   /* Tax infor      */
	  put stream f_salecim unformatted   v_default skip.                                   /* Sales person   */
	  
	  
	  end. /* First-of invoide Head end */
	  
	  /* sales order line */
	  
	  put stream f_salecim unformatted   v_default skip                                    /* Line           */
	                                     v_quote ttsoinvdet.sod_part  v_quote skip         /* Part Number    */
										          v_quote ttsoinvdet.so_site   v_quote skip         /* Site           */
										          v_quote ttsoinvdet.sod_qty_inv v_quote v_space    /* Quantity       */
										          v_default skip                                    /* UM             */
										          v_default skip                                    /*                */
										          v_quote ttsoinvdet.sod_list_pr v_quote v_space    /* Price          */
                                        v_default skip                                    /* Discount       */	
                                        v_default skip                                    /* Net Price      */
                                        v_quote ttsoinvdet.sod_loc v_quote v_space	      /* Location       */
                                        v_default v_space                                 /* Lot Serial     */
                                        v_default v_space								         /* Reference      */
                                       .
       if v_incomeacc <> "" then
       put  stream f_salecim unformatted 
                                         v_quote v_incomeacc v_quote v_space.								   /* Sales acc      */
       else                                    
       put  stream f_salecim unformatted 
                                         v_default v_space.								   /* Sales acc      */  
        put  stream f_salecim unformatted                                          
										 v_default v_space								   /* Sub acc        */
										 v_default v_space								   /* cost center    */
										 v_default v_space								   /* Project        */
                               v_default v_space								   /* Discount acc   */
										 v_default v_space								   /* Sub acc        */
										 v_default v_space								   /* cost center    */
										 v_default v_space								   /* Project        */										 
										 v_default v_space								   /* Requre date    */
										 v_default v_space								   /* Due date       */
										 v_default v_space								   /* perfor date    */
										 v_default v_space								   /* Type           */
										 v_default v_space								   /* Fixed price    */
										 v_default v_space								   /* Transport type */
										 v_default v_space								   /* Converison     */
										 v_default v_space								   /* Forcast        */
										 v_default v_space								   /* Transport price */
										 "Y" v_space								   /* Taxable         */
										 ttsoinvdet.so_taxc v_space		     	       /* Tax rate        */
										 v_quote "N" v_quote v_space skip   			   /* Comment         */
										 v_default v_space		skip   		.	          /* Tax screen        */
										 

	 
	/* Invoice Trail */ 
	if last-of(ttsoinvdet.so_nbr) then do:
	
	put stream f_salecim unformatted   "." skip                                   /* Line end               */
	                                   "." skip .                                 /* single multiple format */
	if v_tottax <> 0 then 
    put stream f_salecim unformatted  v_default v_space								   /*         */
	                                   v_default v_space								   /*         */
									           v_default v_space								   /*         */
									           v_default v_space								   /*         */
									           v_default v_space								   /*         */
									           v_default v_space								   /*         */
									           v_default v_space								   /*         */
	                                   v_quote "Y"   v_quote skip                 /*  Edit Tax detail */
									           v_quote "1" v_quote v_space						/*  Line    */
									           v_default v_space	skip   					   /*  Order Trail       */
									           v_default v_space								   /*  Order Trail       */
									           v_default v_space								   /*  Order Trail       */
									           v_default v_space								   /*  Order Trail       */
									           v_quote v_taxadj v_quote v_space	skip    		   /*  Order Trail       */
									           "."       skip
 									           v_default skip  .
	else
	put stream f_salecim unformatted   v_default v_space skip    						   /*  Tax screen       */
	                                   v_default v_space skip	.						   /*  Trail screen      */

						   
    	   
	
	
	
	output stream f_salecim close.
	output stream f_pscim close.
	   
   cimloadprocess:
   do transaction on error undo cimloadprocess, leave cimloadprocess:   
   
         execname = "xxsoinvapi.p".

        v_runproagram = "soivmt.p".

         run cimloadprocess(input v_salecim,
                            input v_salelog,
                            input v_runproagram,
                            input ttsoinvdet.so_traceid,
                            input ttsoinvdet.so_dataid,
                            input v_sonbr,
                            output v_errstring,
                            output v_errorstatus) .
                            
         v_toterrorstatus = v_toterrorstatus or v_errorstatus.
        /* validation transaction history  */
    
        /* Handle cim and log file */
        do vi_filecount = 1 to 2:
          /* when success move cim log file to archive directory  */
          /* when failed move cim log file to error directory  */   
          /*  move status json file to out directory for API return */   
          /* when success move previous step .p and json file to archive directory  */
          /* when failed move previous step .p and json file to error directory  */       
          case vi_filecount:
          when 1 then
          assign vc_filename = v_salecim
                 vc_topath   = if v_errorstatus then m_errorpath else m_archpath.
          when 2 then
          assign vc_filename = v_salelog
                 vc_topath   = if v_errorstatus then m_errorpath else m_archpath.
           end case.     
           
           
          run cimfilehandle(input vc_filename,
                            input vc_topath,
                            input no ).     



          assign ttsoinvdet.so_cimfile = replace(v_salecim,m_workpath,vc_topath)
                 ttsoinvdet.so_logfile = replace(v_salelog,m_workpath,vc_topath).

                            
                 
       end. /* do vi_filecount = 1 to 2: */   
          
       if not v_errorstatus then 
       do:
         v_runproagram = "soivpst.p".
         
         
         run cimloadprocess(input v_pscim,
                            input v_pslog,
                            input v_runproagram,
                            input ttsoinvdet.so_traceid,
                            input ttsoinvdet.so_dataid,
                            input v_sonbr,
                            output v_errstring,
                            output v_errorstatus) .
                            
         v_toterrorstatus = v_toterrorstatus or v_errorstatus.
        /* validation transaction history  */
    
        /* Handle cim and log file */
        do vi_filecount = 1 to 2:
          /* when success move cim log file to archive directory  */
          /* when failed move cim log file to error directory  */   
          /*  move status json file to out directory for API return */   
          /* when success move previous step .p and json file to archive directory  */
          /* when failed move previous step .p and json file to error directory  */       
          case vi_filecount:
          when 1 then
          assign vc_filename = v_pscim
                 vc_topath   = if v_errorstatus then m_errorpath else m_archpath.
          when 2 then
          assign vc_filename = v_pslog
                 vc_topath   = if v_errorstatus then m_errorpath else m_archpath.
           end case.     
           
           
          run cimfilehandle(input vc_filename,
                            input vc_topath,
                            input no ).     



          assign ttsoinvdet.so_cimfile = replace(v_pscim,m_workpath,vc_topath)
                 ttsoinvdet.so_logfile = replace(v_pslog,m_workpath,vc_topath).

                            
                 
       end. /* do vi_filecount = 1 to 2: */   
          
       end. /*  if not v_errorstatus then  posting */
       
      if not v_errorstatus then 
       do:
       v_invfile = v_invpath + v_sonbr + ".prn".
		/* message v_invfile view-as alert-box. */
		FILE-INFO:filename = (v_invfile).
      if FILE-INFO:file-size > 0 then do:		
        v_errorstatus = no.
        
      v_count = 0.
      
		input from value(v_invfile).    
       invpost:      
       repeat:
       import unformatted l_line.
          v_count = v_count + 1.
           if v_errorstatus and v_count = 2 then 
           do:
                  v_errorcnt = v_errorcnt + 1.
                  create api_status.
                  assign api_status.data_seq     = v_errorcnt
                         api_status.return_status  = "FAILED"
                         api_status.message_type = "Posting-validation"
                         api_status.message_string  = "INVOICE POSTING " + l_line               
                         api_status.dataid  = ttsoinvdet.so_dataid   
                         api_status.billnbr = ttsoinvdet.so_nbr
                         api_status.traceid = vc_traceid 
                         api_status.domain = global_domain.  
                 
                                     
                       leave invpost.
            end.
       
           if (index(l_line, getTermLabel("Error",5)) <> 0 
                 or index(l_line, "**") <> 0)
					 and index(l_line,getTermLabel("C_R_E_D_I_T",6)) = 0 then
                do:
                  v_count = 0.
					   assign 
                         v_errorstatus    = yes
                         v_toterrorstatus = v_toterrorstatus or v_errorstatus.
                  
                  
                  end. /* end if index */ 
                  
           end. /* repeat read log file */               
      input close. /* input from value(v_log). */
       
     end. /* input from value(v_invfile). */
       

          assign vc_filename = v_invfile
                 vc_topath   = if v_errorstatus then m_errorpath else m_archpath.
              
           
           
          run cimfilehandle(input vc_filename,
                            input vc_topath,
                            input no ).     



          assign ttsoinvdet.so_cimfile = replace(v_salecim,m_workpath,vc_topath)
                 ttsoinvdet.so_logfile = replace(v_invfile,m_workpath,vc_topath).

       end. /*  if not v_errorstatus then  */
       
       
       
       if  v_errorstatus then 
          
          undo cimloadprocess, leave cimloadprocess.         
       
       
       end.  /* do on error undo cimloadprocess:   */       
     

	 end. /* if last ttsoinvdet */

   end. /* for each ttsoinvdet */
   
      if can-find(first api_status where api_status.return_status = "FAILED")
      then 
      do:
       
       for each api_status where api_status.return_status = "SUCCESS":
       
        delete api_status.
       
       
       end.
       
       end.
   
      /* Create output json */                   
         run writejsonfile(input hresult,
                  input vc_outputjson,
                  input "FILE",
                  input True,
                  input "UTF-8",
                  output vl_RetOK,
                  output vc_errorstring).
				  
   end. /* if v_import */
   
          do vi_filecount = 1 to 5:
          /* when success move cim log file to archive directory  */
          /* when failed move cim log file to error directory  */   
          /*  move status json file to out directory for API return */   
          /* when success move previous step .p and json file to archive directory  */
          /* when failed move previous step .p and json file to error directory  */       
          case vi_filecount:
      
          when 1 then
          assign vc_filename = replace(vc_inputjson,".json",".p")
                 vc_topath   = if v_errorstatus then m_errorpath else m_archpath. 
          when 2 then
          assign vc_filename = vc_inputjson
                 vc_topath   = if v_errorstatus then m_errorpath else m_archpath. 
          when 3 then
          assign vc_filename = replace(vc_inputjson,".json",".dat")
                 vc_topath   = if v_errorstatus then m_errorpath else m_archpath. 
          when 4 then
          assign vc_filename = replace(vc_inputjson,".json",".run.log")
                 vc_topath   = m_outpath.
          when 5 then
          assign vc_filename = replace(vc_inputjson,".json",".run.out.json")
                 vc_topath   =  m_outpath.
          end case.
          
        
        
        
        
          
          run cimfilehandle(input vc_filename,
                            input vc_topath,
                            input no ).                 
                   
          
     
       end.       /* do vi_filecount = 1 to 5: */  
/*
   /* PRINTER SELECTION */
   {us/gp/gpselout.i &printType = "printer"
   &printWidth = 132
   &pagedFlag = " "
   &stream = " "
   &appendToFile = " "
   &streamedOutputToTerminal = " "
   &withBatchOption = "yes"
   &displayStatementType = 1
   &withCancelMessage = "yes"
   &pageBottomMargin = 6
   &withEmail = "yes"
   &withWinprint = "yes"
   &defineVariables = "yes"}
   
   for each ttsoinvdet break by ttsoinvdet.so_nbr:
   
   if first-of(ttsoinvdet.so_nbr) then 
   do:
   put skip.
  
   put  unformatted ttsoinvdet.so_nbr " ----- "  v_errstring skip.
   put "Customer Invoice Posting Detail:" skip.
   if v_errorstatus then 
   v_invfile = m_outpath + ttsoinvdet.so_nbr + ".prn".
   else 
   v_invfile = m_archpath + ttsoinvdet.so_nbr  + ".prn".
   put unformatted v_invfile skip.
   input from value(v_invfile).
   repeat:
     import unformatted l_line.
	 put  unformatted l_line skip.
   end.
   input close.
   end.
   
   
   end. /* for each ttsoinvdet break by ttsoinvdet.so_nbr:  */
   
  {us/mf/mfrtrail.i}
  */
  leave.
end. /* repeat on error undo mainloop, retry mainloop: */

